//
//  PYCatalogueCell.m
//  IHome
//
//   Created by zhujizheng on 2020/2/18.
//  Copyright © 2019年 com.zhujizheng. All rights reserved.
//

#import "PYCatalogueCell.h"
#import "PYCatalogue.h"

@interface PYCatalogueCell ()

@property (nonatomic, strong) UIImageView *imgView;
@property (nonatomic, strong) UILabel *lbl;
@property (nonatomic, strong) PYCatalogue *data;
@property (nonatomic, weak) id <PYCatalogueCellDelegate> delegate;
@end

@implementation PYCatalogueCell

+ (instancetype)cellWithTableView:(UITableView *)tableView data:(PYCatalogue *)data delegate:(nullable id<PYCatalogueCellDelegate>)delegate
{
    PYCatalogueCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellid"];
    if (!cell) {
        cell = [[PYCatalogueCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cellid"];
        [cell initImgView];
        [cell initLbl];
        [cell initLongPress];
    }
    cell.data = data;
    cell.delegate = delegate;
    return cell;
}

#pragma mark - data
- (void)setData:(PYCatalogue *)data
{
    _data = data;
    if (data.type == PYFileTypeUnknow) {
        self.imgView.image = [UIImage imageNamed:@"file"];
    } else if (data.type == PYFileTypeDirectory) {
        self.imgView.image = [UIImage imageNamed:@"folder"];
    } else if (data.type == PYFileTypeImage) {
        self.imgView.image = [UIImage imageNamed:@"image_file"];
    } else if (data.type == PYFileTypePlist) {
        self.imgView.image = [UIImage imageNamed:@"plist"];
    } else if (data.type == PYFileTypeLog) {
        self.imgView.image = [UIImage imageNamed:@"log"];
    } else if (data.type == PYFileTypeZip) {
        self.imgView.image = [UIImage imageNamed:@"zip"];
    } else if (data.type == PYFileTypeDB) {
        self.imgView.image = [UIImage imageNamed:@"db"];
    } else if (data.type == PYFileTypeSqlite) {
        self.imgView.image = [UIImage imageNamed:@"sqlite"];
    } else if (data.type == PYFileTypeVideo) {
        self.imgView.image = [UIImage imageNamed:@"video"];
    }
    self.lbl.text = data.name;
}

#pragma mark - imgView
- (UIImageView *)imgView
{
    if (!_imgView) {
        _imgView = [UIImageView new];
        _imgView.contentMode = UIViewContentModeScaleAspectFit;
    }
    return _imgView;
}

- (void)initImgView
{
    [self.contentView addSubview:self.imgView];
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.centerY.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
}

#pragma mark - lbl
- (UILabel *)lbl
{
    if (!_lbl) {
        _lbl = [UILabel new];
        _lbl.textColor = PY_Dark_Color;
        _lbl.font = PY_Font_Default;
    }
    return _lbl;
}

- (void)initLbl
{
    [self.contentView addSubview:self.lbl];
    [self.lbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.imgView.mas_right).mas_offset(10);
        make.top.bottom.right.mas_equalTo(0);
    }];
}

#pragma mark - 菜单
- (BOOL)canBecomeFirstResponder
{
    return YES;
}

- (void)initLongPress
{
    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self addGestureRecognizer:longPress];
}

/**
 * 这个方法决定了MenuController的菜单项内容
 * 返回YES，就代表MenuController会有action菜单项
 * action == @selector(cut:) || action == @selector(copy:) || action == @selector(paste:) || action == @selector(select:) || action == @selector(selectAll:) || action == @selector(delete:)
 */
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    if (action == @selector(deleteFile:)) {
        // 需要展示的系统菜单，改变系统菜单文本语言 PROJECT-Locallzations-添加Chinese(Simplified)
        return YES;
    }
    return NO;
    /*
     // 禁用所有长按文本框操作
     if ([UIMenuController sharedMenuController]) {
     [UIMenuController sharedMenuController].menuVisible = NO;
     }
     return NO;
     */
}

- (void)longPress:(UILongPressGestureRecognizer *)longPress {
    // 让该控件成为第一响应者, 还需要实现方法canBecomeFirstResponder
    [self becomeFirstResponder];
    if (longPress.state == UIGestureRecognizerStateBegan) {
        UIMenuController *popMenu = [UIMenuController sharedMenuController];
        UIMenuItem *deleteItem = [[UIMenuItem alloc] initWithTitle:@"删除" action:@selector(deleteFile:)];
        [popMenu setMenuItems:[NSArray arrayWithObjects:deleteItem, nil]];
        // 方向
        [popMenu setArrowDirection:UIMenuControllerArrowDefault];
        // 部分选取
        [popMenu setTargetRect:self.bounds inView:self.contentView];
        // 设置菜单中可见
        [popMenu setMenuVisible:YES animated:YES];
        
        NSLog(@"ddd");
    }
}

- (void)deleteFile:(UIMenuItem *)menuItem
{
    NSFileManager *manager = [NSFileManager defaultManager];
    NSError *error = nil;
    BOOL success = [manager removeItemAtPath:self.data.fullPath error:&error];
    if (success) {
        if ([self.delegate respondsToSelector:@selector(cell:deleteCatalogue:)]) {
            [self.delegate cell:self deleteCatalogue:self.data];
        }
    } else {
        [MBProgressHUD py_showText:error.localizedFailureReason];
        NSLog(@"删除文件失败:%@", self.data.fullPath);
    }
    
}

@end
